package com.example.stmall.manager.service.impl;

import com.example.stmall.manager.entity.Admin;
import com.example.stmall.manager.mapper.AdminMapper;
import com.example.stmall.manager.service.AdminService;
import com.example.stmall.manager.util.MD5Util;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@AllArgsConstructor
public class AdminServiceImpl implements AdminService {

    /**
     * Spring4.x支持构造方法注入
     * 提供一个有参数的构造方法,不需要引来任何的注解
     */
    private AdminMapper adminMapper;

/*    public AdminServiceImpl(AdminMapper adminMapper){
        this.adminMapper = adminMapper;
    }*/


    @Override
    public Admin login(Admin admin) throws Exception {
        //1.参数的验证
        if(admin.getUsername() == null ||
            admin.getUsername().length() < 3||
            admin.getUsername().length() > 16){
            throw new Exception("用户名必须是3~16位的字符串");
        }
        if(admin.getPassword() == null ||
            admin.getPassword().length() <3 ||
            admin.getPassword().length() >16){
            throw new Exception("密码必须是3~16位的字符串");
        }
        //根据用户名查询
        Admin sAdmin = adminMapper.selectByUsername(admin.getUsername());
        if(sAdmin == null){
            throw new Exception("用户名不存在！！！");
        }
        //验证密码
        String password = admin.getPassword();
        password = MD5Util.getMD5(password + sAdmin.getSalt());
        if(!password.equals(sAdmin.getPassword())){
            throw new Exception("密码错误~~~");
        }
        //判断状态
        if(sAdmin.getStatus() == 1){
            throw new Exception("用户已被禁用！");
        }
        //2.业务逻辑
        //3.访问数据库
        return sAdmin;
    }

    @Override
    public Admin modifyPwd(Integer id, String oldPassword, String newPassword, String rePassword) throws Exception {
        return null;
    }

    @Override
    public Admin edit(Admin admin) throws Exception {
        return null;
    }


}
